const koa = require('koa');
const static = require('koa-static');
const Router = require('koa-router');
const mysql2 = require('mysql2');
const koaBody = require('koa-body')

let app = new koa();
let router = new Router();

app.use(static(__dirname + '/static'));
app.use(koaBody({
  multipart: true // 允许上传文件
}));

const server = require('http').createServer(app.callback());

const io = require('socket.io')(server);

const connection = mysql2.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'test'
});

router.get("/", ctx => {
  ctx.body = "some value...";
});

app.use(router.routes());

io.on('connection', socket => {
  console.log('有socket连接');

  socket.on('addData', function (data) {
    console.log(data, '客户端来的信息');
    socket.broadcast.emit('addInputData', data);
    socket.local.emit('addInputData', data);
  })
})

server.listen(3000);